# -*- coding: utf-8 -*-
# @Time : 2024/3/22 17:26
# @Author : debbyszhang
# @File : test1.py
# @desc :
import json

from xmindparser import xmind_to_dict

def count_markers(json_file):
    # 读取JSON文件
    with open(json_file, 'r') as f:
        json_data = json.load(f)

    # 统计makers数组中的元素及其个数
    markers_count = {}
    topics = json_data[0]['topic']['topics']
    for topic in topics:
        if 'makers' in topic:
            markers = topic['makers']
            for marker in markers:
                if marker in markers_count:
                    markers_count[marker] += 1
                else:
                    markers_count[marker] = 1

    return markers_count

def print_topics(xmind_file, indent=0):
    # 将Xmind文件解析为字典
    xmind_dict = xmind_to_dict(xmind_file)

    # 获取根主题
    root_topic = xmind_dict[0]['topic']

    # 打印当前主题的内容
    print(' ' * indent + root_topic['title'])


    # 递归遍历子主题
    for sub_topic in root_topic.get('topics', []):
        print_topics_recursive(sub_topic, indent + 2)

markers_count = {}

def print_topics_recursive(topic, indent):
    # 打印当前主题的内容
    print(' ' * indent + topic['title'])
    if 'makers' in topic:
        markers = topic['makers']
        for marker in markers:
            if marker in markers_count:
                markers_count[marker] += 1
            else:
                markers_count[marker] = 1
    # 递归遍历子主题
    for sub_topic in topic.get('topics', []):
        if 'makers' in sub_topic:
            markers = sub_topic['makers']
            for marker in markers:
                if marker in markers_count:
                    markers_count[marker] += 1
                else:
                    markers_count[marker] = 1
        print_topics_recursive(sub_topic, indent + 2)



xmind_file = '/Users/debby/Documents/用例步骤梳理/开放联盟链-主子链/开放联盟链-主子链.xmind'
# xmind_file = '/Users/debby/Documents/用例步骤梳理/开放联盟链-主子链/111.xmind'

print_topics(xmind_file)

print("\n*****************\n")
for marker, count in markers_count.items():
    print(f"{marker}: {count}")